package com.landiao.common.utils;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * 日期处理工具类
 */
public class DateUtil
{
	/**默认显示日期的格式:yyyy-MM-dd*/
	public static final String DATAFORMAT_STR = "yyyy-MM-dd";
	public static final String DAYTIMEFORMAT_STR = "ddHHmmss";
	public static final String DATETIMEFORMAT_STR = "yyyyMMddHHmmss";
	public static final String YYYYMMDDHH = "yyyyMMddHH";
	public static final String YYYYMMDD = "yyyyMMdd";
	public static final String YYMMDDHHmmSSsss = "yyMMddHHmmssSSS";
	public static final String MMDDHHmm = "M月d日 HH:mm";

	public static final String todaySdf = "HH:mm";
	public static final String yesterdaySdf = "HH:mm 昨天 ";
	public static final String otherSdf = "HH:mm MM-dd";

	/**
	 * 字符串转为日期
	 * @param str
	 * @param format
	 * @return
	 */
	public static Date str2Date(String str, String format) {
		if (null == str || "".equals(str)) {
			return null;
		}
		Date date = null;
		try {
			date = new SimpleDateFormat(format).parse(str);
			return date;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 日期转换为字符串
	 * 
	 * @param date
	 *            日期
	 * @param format
	 *            日期格式
	 * @return 字符串
	 */
	public static String date2Str(Date date, String format) {
		if (null == date) {
			return null;
		}
		return new SimpleDateFormat(format).format(date);
	}

	// 将传入时间与当前时间进行对比，是否今天昨天  
	public static String changeTime(Date date) {
		SimpleDateFormat sfd = null;  
		String time = "";  
		Calendar dateCalendar = Calendar.getInstance();  
		dateCalendar.setTime(date);  
		Date now = new Date();  
		Calendar targetCalendar = Calendar.getInstance();  
		targetCalendar.setTime(now);  
		targetCalendar.set(Calendar.HOUR_OF_DAY, 0);  
		targetCalendar.set(Calendar.MINUTE, 0);  
		if (dateCalendar.after(targetCalendar)) {  
			sfd = new SimpleDateFormat(todaySdf);  
			time = sfd.format(date);  
			return time;  
		} else {  
			targetCalendar.add(Calendar.DATE, -1);  
			if (dateCalendar.after(targetCalendar)) {  
				sfd = new SimpleDateFormat(yesterdaySdf);  
				time = sfd.format(date);  
				return time;  
			}  
		}  
		sfd = new SimpleDateFormat(otherSdf);  
		time = sfd.format(date);  
		return time;  
	}  

	/**
	 * 计算两个时间之间的天数
	 * @param c1 减数
	 * @param c2 被减数
	 * @return
	 */
	public static int DateDiff(Calendar c1,Calendar c2)
	{
		return DateDiff(c1,c2,"D");
	}

	/**
	 * 计算两个时间之间的差值，根据标志的不同而不同
	 * @param c1
	 * @param c2
	 * @param dateUtil 标志，表示按照月M/日D/时H/分m/秒s等计算
	 * @return
	 */
	public static int DateDiff(Calendar c1,Calendar c2,String dateUtil)
	{
		return DateDiff(c1.getTime(),c2.getTime(),dateUtil);
	}

	/**
	 * 计算两个时间之间的天数
	 * @param d1 减数
	 * @param d2 被减数
	 * @return
	 */
	public static int DateDiff(Date d1,Date d2){
		return DateDiff(d1,d2,"D");
	}

	/**
	 * 计算两个时间之间的差值，根据标志的不同而不同
	 * @param d1
	 * @param d2
	 * @param dateUnit 标志，表示按照月(M)/日(D)/时(H)/分(m)/秒(s)等计算
	 * @return
	 */
	public static int DateDiff(Date d1,Date d2,String dateUnit)
	{		
		int int_return;
		try {
			long l = d1.getTime() - d2.getTime();
			int_return = 0;
			if(dateUnit.equals("s"))
				int_return = (int)(l / (1000));
			if(dateUnit.equals("m"))
				int_return = (int)(l / (1000 * 60));
			if(dateUnit.equals("H"))
				int_return = (int)(l / (1000 * 60 * 60));
			if(dateUnit.equals("D"))
				int_return = (int)(l / (1000 * 60 * 60 * 24));
			if(dateUnit.equals("M"))
			{			
				int_return = (d1.getYear()-d2.getYear())*12 + 
						(d1.getMonth()-d2.getMonth())-1 + 
						((d1.getMonth()-d2.getMonth())==0?(d1.getDate()>=d2.getDate()?1:0):(d1.getDate()>=d2.getDate()?1:0));
			}
		} catch (RuntimeException e) {

			return 0;
		}
		return int_return;
	}

	/**
	 * 
	 * 查询时用的开始时间
	 * @param date
	 * @return
	 * @exception
	 */
	public static Timestamp getStartTime(Date date) {
		if (date != null) {
			Calendar dateCalendar = Calendar.getInstance();
			dateCalendar.setTime(date);
			Calendar calendar = new GregorianCalendar(dateCalendar.get(Calendar.YEAR),
					dateCalendar.get(Calendar.MONTH), dateCalendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
			return new Timestamp(calendar.getTimeInMillis());
		} else {
			return null;
		}
	}
	/**
	 * 
	 * Description: 查询时用的结束时间
	 * @param date
	 * @return
	 * @exception
	 */
	public static Timestamp getEndTime(Date date) {
		if (date != null) {
			Calendar dateCalendar = Calendar.getInstance();
			dateCalendar.setTime(date);
			Calendar calendar = new GregorianCalendar(dateCalendar.get(Calendar.YEAR),
					dateCalendar.get(Calendar.MONTH), dateCalendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
			return new Timestamp(calendar.getTimeInMillis());
		} else {
			return null;
		}
	}

	/** 
	 * 得到几天前的时间 
	 * @param d 
	 * @param day 
	 * @return 
	 */  
	public static Date getDateBefore(Date d,int day){  
		Calendar now =Calendar.getInstance();  
		now.setTime(d);  
		now.set(Calendar.DATE,now.get(Calendar.DATE)-day);  
		return now.getTime();  
	}  

	/** 
	 * 得到几天后的时间 
	 * @param d 
	 * @param day 
	 * @return 
	 */  
	public static Date getDateAfter(Date d,int day){  
		Calendar now =Calendar.getInstance();  
		now.setTime(d);  
		now.set(Calendar.DATE,now.get(Calendar.DATE)+day);  
		return now.getTime();  
	}  

	/**
	 * 获取N月后第一天,N支持正负
	 * @return
	 */
	public static Date getMonthFirstDate(Date d,int month){
		Calendar curCal = Calendar.getInstance();
		curCal.setTime(d);
		curCal.add(Calendar.MONTH, month);
		curCal.set(Calendar.DAY_OF_MONTH, 1);
		Date beginTime = curCal.getTime();
		return beginTime;
	}
	/**
	 * 获取N月后最后一天,N支持正负
	 * @return
	 */
	public static Date getMonthLastDate(Date d,int month){
		Calendar curCal = Calendar.getInstance();
		curCal.setTime(d);
		curCal.add(Calendar.MONTH, month);
		curCal.set(Calendar.DATE, 1);
		curCal.roll(Calendar.DATE, -1);
		Date endTime = curCal.getTime();
		return endTime;
	}
	
	public static void main(String[] args) {
System.out.println(getMonthLastDate(getStartTime(new Date()), 0));
System.out.println(getStartTime(getMonthFirstDate(new Date(), 0)));
System.out.println(getStartTime(getDateBefore(new Date(), 7)));

	}
}

